import { ATeamCombat } from "GameLoop/WorldState/EntityManager/Entitys";
import { PositionHelper } from "utils/position/PositionHelper";
import { IDefendStrategy } from "./Base";
import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";

/**
 * 普通外矿防守
 */
export class  DefendStrategy1 extends IDefendStrategy
{
    public assignTarget(room:Room,task:ITask<ITeamTaskData<"defend">>,teams:ATeamCombat[],creeps:Creep[]): ETaskState 
    {
        const workroom = Game.rooms[task.data.room];
          // 设置移动目标,测试
        const goal = this.getMoveGoal(task.data.room,workroom);

        if(workroom)
        {
            const cachehostiles = workroom.getWorld().PollHostiles(workroom.name);
            if(!cachehostiles)
            {
                // 错误，没有获取道敌方单位
                room.log("error","在拥有视野的情况下没有获取道敌方爬的缓存",workroom.name);
                return   ETaskState.Running;
            }
            const enemy:Creep[] = [];
            for(const creepid of cachehostiles.cacheData)
            {
                const e = Game.getObjectById(creepid.id);
                if(e)
                {
                    enemy.push(e);
                }
            }
            // 同时对房间环境进行分析
            // const groups = WarStrategy.classifyEnemy(enemy);
            
            task.data.arg.enemy = enemy.map(e=>e.id);
            task.data.arg.structures = task.data.arg.structures.filter(e=>Game.getObjectById(e));
            if( task.data.arg.structures .length==0 && !workroom.IsMy())
            {
                const sstructures = workroom.find(FIND_STRUCTURES,{filter:STRUCTURE_INVADER_CORE}) ;
                task.data.arg.structures  = sstructures.map(e=>e.id);
            }
            this.updateTeamAroundHostile(teams,enemy);
            this.updateCreepAroundHostile(creeps,enemy);
            if(task.data.arg.enemy.length>0)
            {
                this.setCreepTactic(creeps,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(enemy[0])]},task.data.room,this.correctCreep);
                this.setTeamTractic(teams,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(enemy[0])]},task.data.room,this.correctTeam);
            }else  if(task.data.arg.structures.length>0) 
            {
                this.setCreepTactic(creeps,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(task.data.arg.structures[0])]},task.data.room);
                this.setTeamTractic(teams,{goal:goal,type:2,targets:[PositionHelper.getObjectRef(task.data.arg.structures[0])]},task.data.room);
            }
            else 
            {
                room.log("info",`当前房间已经没有敌方单位`)
                let idx = task.data.arg.benchRooms.findIndex(e=>e == task.data.room);
                if(idx > -1)
                {
                    // 
                    task.data.arg.benchRooms.splice(idx,1);
                }
                // 还有没有待检测的房间
                if(task.data.arg.benchRooms.length)
                {
                    // 有
                    task.data.room = task.data.arg.benchRooms[0];
                }
                else 
                {
                    // 无则完成
                    return ETaskState.Complete
                }
            }
            
            // this.deployStrategy1(teams,task.data);
        }
        return ETaskState.Running
    }

}